/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/


#pragma once

/*-----------------------------------------------------------------------------
	game class itself :
-----------------------------------------------------------------------------*/

const uint GAME_MAX_ENTITIES	=	65536;


class EGame : public IGame {
	public:
						EGame			( void );
						~EGame			( void );
		virtual void	Frame			( uint dtime );

	protected:
		bool			is_game_running;
		float			game_time;
	
		void			InitGameAPI		( void );
		void			RunGame			( const EString level );
		void			EndGame			( void );
		void			EndGameFailure	( const exception &e );
		

		//
		//	Level stuff :
		//		
		void			LevelLoad		( const EString level );
		void			LevelClean		( void );
		
		struct {
			EString					name;
			IPxScene				scene;
			float					gravity;
			std::vector<IPxFREntity>		rend_meshes;
			std::vector<IPxPhysEntity>	phys_meshes;
		} level;
		
		IPxRenderSystem	rs;
		IPxFRScene		fr_scene;
		IPxDVScene		dv_scene;
		
		IPxFRLight		light, light2;


		uint			game_frames;

		HardRef<EGEntity>	entities[GAME_MAX_ENTITIES];

		// Camera animation stuff
		IPxAnimTrack	camera_anim;
		bool			camera_lock;

	public:
		HardRef<EGWaving>	waving;
		
	public:
		//	game API :
		static int	runGame			( lua_State *L );
		static int	endGame			( lua_State *L );
		static int	purgeCachedData	( lua_State *L );
		
		static int	setView			( lua_State *L );
		static int	setProj			( lua_State *L );
		static int	addRendMesh		( lua_State *L );
		static int	addPhysMesh		( lua_State *L );

		static int	playCameraAnim	( lua_State *L );
		static int	stopCameraAnim	( lua_State *L );
		
		static int	spawnEntity		( lua_State *L );
		static int	killEntity		( lua_State *L );

		static int	traceRay		( lua_State *L );
		static int	traceBox		( lua_State *L );
		static int	overlap			( lua_State *L );
		static int	setGravity		( lua_State *L );
		static int	getGravity		( lua_State *L );
		
		static int	setWaving		( lua_State *L );
		
		static int	setAmbient		( lua_State *L );
		
		//	entity API :
		static int	setPose			( lua_State *L );
		static int	getPose			( lua_State *L );
		static int	setupRigidBody	( lua_State *L );
		static int	setupRendModel	( lua_State *L );
		static int	setupCharacter	( lua_State *L );
		static int	setupShip		( lua_State *L );
		static int	setupTrigger	( lua_State *L );
		
		static int	getTable		( lua_State *L );
		static int	setTable		( lua_State *L );
		static int	isDead			( lua_State *L );
		
		//	rendmodel API :
		static int	playAnim			( lua_State *L );	//	id
		static int	setIKTarget		( lua_State *L );	//	id, target_id, joint, x,y,z
		
		
		//	character API :
		static int	jump			( lua_State *L );
		static int	move			( lua_State *L );
		static int	crouch			( lua_State *L );
		static int	stand			( lua_State *L );
		
		//	rigid body API :
		static int	addImpulse		( lua_State *L );
		
		static int	addForce		( lua_State *L );

		static int	getLocalCamera	( lua_State *L );
		
		static int	getDynamics		( lua_State *L );
		static int	updateForces	( lua_State *L );
		
		static uint			checkEntityID	( lua_State *L, bool check_dead=true );
		static EHrGEntity	getEntity		( lua_State *L );
		
		static EGame	*self;
	};
